
import random as r
import string
import time
import pymysql
import random


def random_name():
    # 删减部分，比较大众化姓氏
    firstName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻水云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅卞齐康伍余元卜顾孟平" \
                "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计成戴宋茅庞熊纪舒屈项祝董粱杜阮席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田胡凌霍万柯卢莫房缪干解应宗丁宣邓郁单杭洪包诸左石崔吉" \
                "龚程邢滑裴陆荣翁荀羊甄家封芮储靳邴松井富乌焦巴弓牧隗山谷车侯伊宁仇祖武符刘景詹束龙叶幸司韶黎乔苍双闻莘劳逄姬冉宰桂牛寿通边燕冀尚农温庄晏瞿茹习鱼容向古戈终居衡步都耿满弘国文东殴沃曾关红游盖益桓公晋楚闫"
    # 百家姓全部姓氏
    # firstName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平" \
    #             "黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董粱杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮" \
    #             "龚程嵇邢滑裴陆荣翁荀羊於惠甄麴家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阴欎胥能苍" \
    #             "双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍舄璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空" \
    #             "曾毋沙乜养鞠须丰巢关蒯相查後荆红游竺权逯盖益桓公晋楚闫法汝鄢涂钦归海帅缑亢况后有琴梁丘左丘商牟佘佴伯赏南宫墨哈谯笪年爱阳佟言福百家姓终"
    # 百家姓中双姓氏
    firstName2 = "万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于单于太叔申屠公孙仲孙轩辕令狐钟离宇文长孙慕容鲜于闾丘司徒司空亓官司寇仉督子颛孙端木巫马公西漆雕乐正壤驷公良拓跋夹谷宰父谷梁段干百里东郭南门呼延羊舌微生梁丘左丘东门西门南宫南宫"
    # 女孩名字
    girl = '秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽'
    # 男孩名字
    boy = '伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘'
    # 名
    name = '中笑贝凯歌易仁器义礼智信友上都卡被好无九加电金马钰玉忠孝'
    # 10%的机遇生成双数姓氏
    if random.choice(range(100)) > 10:
        firstName_name = firstName[random.choice(range(len(firstName)))]
    else:
        i = random.choice(range(len(firstName2)))
        firstName_name = firstName2[i:i + 2]

    sex = random.choice(range(2))
    if sex > 0:
        last_name = girl[random.choice(range(len(girl)))]
    else:
        last_name = boy[random.choice(range(len(boy)))]
    return firstName_name, last_name

def phone():
    # 随机手机号
    prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
               "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
               "185", "187", "188", "189"]
    # print(random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8)))
    num2 = r.choice(prelist) + ''.join(r.choice('0123456789') for i in range(8))
    return num2

def addres():
    # 随机地址
    preaddres = ["北京","天津","上海","重庆","河北","河南",
                 "云南","辽宁","黑龙江","湖南","安徽","山东","新疆","江苏","浙江","江西"
                 ,"湖北","广西","甘肃","山西","内蒙古","陕西","吉林","福建","贵州","广东","青海","西藏","四川",
                 "宁夏","海南","台湾","香港","澳门"]
    num2 = r.choice(preaddres)
    return num2

# 生成随机用户名
def generate_random_string(length):
    letters = string.ascii_letters
    if length < 6 or length > 10:
        raise ValueError("Length should be between 6 and 10")
    if length > len(letters):
        raise ValueError("Length is too long for unique letters")

    random_string = ''.join(random.sample(letters, length))
    return random_string


a1=(1976,1,1,0,0,0,0,0,0) #设置开始日期时间元组(1976-01-01 00：00：00)

a2=(2010,12,31,23,59,59,0,0,0) #设置结束日期时间元组(1990-12-31 23：59：59)

start=time.mktime(a1) #生成开始时间戳

end=time.mktime(a2) #生成结束时间戳

#随机生成10个日期字符串

name=[]
randomUserName=[]
passwd1=[]
gender1=[1,2]
for i in range(1000):
    firstName_name, last_name = random_name()
    name2=(firstName_name + last_name).rstrip() #去掉末尾空格后的名字
    if name2 not in name: #名字存入列表中，且没有重名
        name.append(name2)
    random_length = random.randint(6, 10)
    random_string = generate_random_string(random_length)
    if random_string not in randomUserName:
        randomUserName.append(random_string)

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456',db='worldcup')
cur = conn.cursor()
for i in range(97, 123):
    passwd1.append(chr(i))  # 得到字母字符并放入列表
for i in range(48, 58):
    passwd1.append(chr(i))  # 得到数字字符并放入列表
for i in range(len(name)): #插入数据
    age=random.randint(10,100)
    vote=random.randint(1,32)
    t = random.randint(start, end)  # 在开始和结束时间戳中随机取出一个
    date_touple = time.localtime(t)  # 将时间戳生成时间元组
    date = time.strftime("%Y-%m-%d", date_touple)  # 将时间元组转成格式化字符串(1976-05-21)
    str1 = ''.join(random.choices(passwd1, k=8))  # 随机生成8位密码的列表，并将其转换成字符串的形式
    gender=r.choice(gender1)
    cur.execute("insert into user(nike_name,user_password,gender,birthday,age,vote_team,phone,address,user_name) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)",(name[i],str1,gender,date,age,vote,phone(),addres(),randomUserName[i]))#注意用法

cur.execute('select * from user') #查询数据

for s in cur.fetchall():
    print(s)

conn.commit()
cur.close()
conn.close()